/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月4日
 */
package com.massyframework.beanskin.service;

import java.util.HashMap;
import java.util.Map;

import com.massyframework.beanskin.util.version.Version;

/**
 * {@link ServicePropertiesBuilder} 提供服务属性的构建
 */
public class ServicePropertiesBuilder {

    private Map<String, Object> props;

    /**
     * 新建{@link ServicePropertiesBuilder}
     * @return {@link ServicePropertiesBuilder}
     */
    public static ServicePropertiesBuilder newInstance(){
        return new ServicePropertiesBuilder();
    }

    /**
     * 使用<code>svcProps</code>新建{@link ServicePropertiesBuilder}
     * @param props {@link Map}, 服务属性
     * @return {@link ServicePropertiesBuilder}
     */
    public static ServicePropertiesBuilder newInstance(Map<String, Object> props){
        if (props == null){
            return new ServicePropertiesBuilder();
        }
        return new ServicePropertiesBuilder(props);
    }

    /**
     * 构造方法
     */
    protected ServicePropertiesBuilder(){
        this.props = new HashMap<String, Object>();
    }

    /**
     * 构造方法
     * @param props {@link Map}, 服务属性
     */
    protected ServicePropertiesBuilder(Map<String, Object> props){
        this.props = props == null ?
        		new HashMap<>() :
        			new HashMap<String, Object>(props);
    }

    /**
     * 根据<code>propName</code>返回属性
     * @param propName {@link String}, 属性名
     * @return {@link Object}
     */
    public Object getProperty(String propName){
        if (propName == null) return null;
        return this.props.get(propName);
    }

    /**
     * 根据<code>propName</code>按指定类型的返回属性
     * @param propName {@link String}, 属性名
     * @param propType  {@link Class}, 属性类型
     * @return {@link Object}
     */
    public <P> P getProperty(String propName, Class<P> propType){
        Object result = this.getProperty(propName);
        return result == null ? null : propType.cast(result);
    }

    /**
     * 设置允许访问服务的模块所属组编号
     * @param domain {@link String}, 域名
     * @return {@link ServicePropertiesBuilder}, 服务属性构建器
     */
    public ServicePropertiesBuilder allowGroupId(String groupId){
        return this.allowGroupIds(new String[]{groupId});
    }

    /**
     * 设置允许访问服务的模块所属组编号
     * @param domains {@link String}数组, 多个域名
     * @return {@link ServicePropertiesBuilder}, 服务属性构建器
     */
    public ServicePropertiesBuilder allowGroupIds(String[] groupIds){
        this.props.put(ServiceReference.ALLOW_ACCESS_GROUPID, groupIds);
        return this;
    }

    /**
     * 设置服务名称
     * @param value {@link String}. 服务名称
     * @return {@link ServicePropertiesBuilder}
     */
    public ServicePropertiesBuilder name(String value){
        this.props.put(ServiceReference.SERVICE_NAME, value);
        return this;
    }

    /**
     * 设置服务说明
     * @param value {@link String}. 服务说明
     * @return {@link ServicePropertiesBuilder}
     */
    public ServicePropertiesBuilder description(String value){
        if (value == null) return this;

        this.props.put(ServiceReference.SERVICE_DESCRIPTION, value);
        return this;
    }

    /**
     * 设置注册服务实例是否为ServiceFactory。
     * @param value {@link boolean}
     * @return {@link ServicePropertiesBuilder}
     */
    public ServicePropertiesBuilder isFactory(boolean value){
        this.props.put(ServiceReference.SERVICE_ISFACTORY, value);
        return this;
    }

    /**
     * 设置服务排名
     * @param value {@link int}. 服务排名
     * @return {@link ServicePropertiesBuilder}
     */
    public ServicePropertiesBuilder ranking(int value){
        this.props.put(ServiceReference.SERVICE_RANKING, value);
        return this;
    }

    /**
     * 设置服务顶层排名
     * @return {@link ServicePropertiesBuilder}
     */
    public ServicePropertiesBuilder topRanking(){
        this.props.put(ServiceReference.SERVICE_RANKING, Integer.MAX_VALUE);
        return this;
    }

    /**
     * 设置版本
     * @param value {@link String}
     * @return {@link ServicePropertiesBuilder}
     */
    public ServicePropertiesBuilder version(String value){
        this.props.put(ServiceReference.SERVICE_VERSION, value);
        return this;
    }

    /**
     * 设置版本
     * @param version {@link String}
     * @return {@link ServicePropertiesBuilder}
     */
    public ServicePropertiesBuilder version(Version version){
        if (version != null){
            this.props.put(ServiceReference.SERVICE_VERSION, version.toString());
        }
        return this;
    }

    /**
     * 设置服务属性
     * @param name {@link String}, 属性名
     * @param value {@link Object}, 属性值
     * @return {@link ServicePropertiesBuilder}
     */
    public ServicePropertiesBuilder property(String name, Object value){
        this.props.put(name, value);
        return this;
    }

    /**
     * 构建服务属性
     * @return {@link Map}
     */
    public Map<String, Object> build(){
    	Object value = this.props.get(ServiceReference.SERVICE_VERSION);
    	if (value != null) {
    		if (value instanceof String) {
    			this.props.put(ServiceReference.SERVICE_VERSION, Version.valueOf((String)value));
    		}
    	}
    	
        return this.props;
    }
}

